//
//  String+Extension.swift
//

import UIKit
import CommonCrypto

extension String {
    // substring from start index to end index
    // eg:
    // "hello world!" substring(startIndex: 1, endIndex: 5)
    // "ello "
    func substring(startIndex: Int, endIndex: Int) -> String {
        if count > endIndex, startIndex >= 0, endIndex >= startIndex, !isEmpty {
            let startIdx = index(self.startIndex, offsetBy: startIndex)
            let endIdx = index(self.startIndex, offsetBy: endIndex)
            let subString = self[startIdx...endIdx]
            return String(subString)
        }

        return self
    }

    // substring from start index bt length
    // eg:
    // "hello world!" substringFrom(to: 4)
    // "o world"
    func substringFrom(to end: Int) -> String {
        if count > end, end >= 0, !isEmpty {
            let startIdx = index(startIndex, offsetBy: end)
            let endIdx = index(startIndex, offsetBy: count - 1)
            let subString = self[startIdx...endIdx]
            return String(subString)
        }

        return self
    }

    // substring from start index bt length
    // eg:
    // "hello world!" substringBy(length: 6)
    // "hello "
    func substringBy(length: Int) -> String {
        if count >= length, length > 0 {
            let endIdx = index(startIndex, offsetBy: length - 1)
            let subString = self[self.startIndex...endIdx]
            return String(subString)
        }

        return self
    }

    // substring from end by length
    // eg:
    // "hello world!" substringFromEndBy(length: 5)
    // "orld!"
    func substringFromEndBy(length: Int) -> String {
        if count >= length {
            let startIdx = index(startIndex, offsetBy: count - length)
            let endIdx = index(startIdx, offsetBy: length - 1)
            let subString = self[startIdx...endIdx]
            return String(subString)
        }

        return self
    }
    
    /// 截取关键字前面的（不包含关键字）
    func prefix(_ keywords: String) -> String {
        guard let range = range(of: keywords) else { return self }
        let nsRange = NSRange(range, in: keywords)
        return String(prefix(nsRange.location))
    }
    
    /// 截取关键字后面的（包含关键字）
    func suffix(_ keywords: String) -> String {
        guard let range = range(of: keywords) else { return self }
        let nsRange = NSRange(range, in: keywords)
        return String(suffix(self.count - nsRange.location - nsRange.length))
    }
}

public extension String {
    /// 从url中获取后缀 例：.pdf
    var pathExtension: String {
        guard let url = URL(string: self) else { return "" }
        return url.pathExtension.isEmpty ? "" : ".\(url.pathExtension)"
    }
}

public extension String {
    var MD5String: String {
        let cStrl = cString(using: .utf8)
        let strLen = CUnsignedInt(lengthOfBytes(using: .utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        
        let buffer = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        CC_MD5(cStrl, strLen, buffer)
        var md5String = ""
        for idx in 0..<digestLen {
            let obcStrl = String.init(format: "%02x", buffer[idx])
            md5String.append(obcStrl)
        }
        free(buffer)
        return md5String
    }
}
